var baseInfoForXlsImportWin;
var baseInfoForXlsSelectStore;
Ext.onReady(function() {

	baseInfoForXlsImportWin = baseInfoForXlsImportWin();

	/**
	 * Excel文件类别的下拉框
	 */
	var typeCombox = new Ext.form.ComboBox({
				fieldLabel : 'Excel文件类别',
				store : Ext.baseInfoForXlsImport.typeStoreForSelect,
				displayField : 'ex_publicname',
				valueField : 'ex_publictype',
				typeAhead : true,
				id : 'exPublicTypeSelect',
				mode : 'local',
				triggerAction : 'all',
				emptyText : '请选择...',
				selectOnFocus : true,
				anchor : '90%',
				width : 150
			});

	/**
	 * 是否有效的下拉框
	 */
	var isUsedCombox = new Ext.form.ComboBox({
				fieldLabel : '是否有效',
				store : Ext.baseInfoForXlsImport.isUsedStoreForSelect,
				displayField : 'isUsed',
				id : 'isUsedSelect',
				valueField : 'isUsed_id',
				typeAhead : true,
				mode : 'local',
				triggerAction : 'all',
				emptyText : '请选择...',
				selectOnFocus : true,
				anchor : '90%',
				width : 150
			});

	var baseInfoForXlsSelectPanel = new Ext.form.FormPanel({
		
		frame : true,
		hideLables : false,
		id : 'addPanelForm',
		buttonAlign : 'right',
		bodyStyle : 'padding:5px 5px 0',
		items : [{
					layout : 'column',
					items : [{
								columnWidth : .5,
								layout : 'form',
								items : [{
											layout : 'form',
											items : [typeCombox]
										}, {
											layout : 'form',
											items : [isUsedCombox]

										}]
							}, {
								columnWidth : .5,
								layout : 'form',
								items : [{
											xtype : 'textfield',
											fieldLabel : '固定项目名称',
											name : 'exPublicName',
											id : 'exPublicName',
											anchor : '90%'
										}]

							}]

				}],
		buttons : [{
			text : '查询',
			handler : function() {
				baseInfoForXlsSelectStore.on('beforeload', function() {
							baseInfoForXlsSelectStore.baseParams = {
								exPublicType : typeCombox.getValue(),
								isUsed : isUsedCombox.getValue(),
								exPublicName : baseInfoForXlsSelectPanel
										.findById('exPublicName').getValue()
							}
						});
				baseInfoForXlsSelectStore.reload();
			}
		}, {
			text : '新增',
			handler : function() {
				modifyBaseInfo(null, null, null, null, null);
			}
		}, {
			text : '删除',
			handler : function() {
				var length = baseInfoForXlsSelectGrid.getSelectionModel()
						.getSelections().length
				if (length < 1) {
					Ext.Msg.alert('警告', '请选择需要删除的项目');
					return;
				}
				Ext.MessageBox.show({
					title : '提示',
					msg : '您选中了' + length + '条记录进行删除,请问您确认吗?',
					buttons : Ext.MessageBox.OKCANCEL,
					fn : function(btn) {
						if (btn == "ok") {

							var exPublicIds = [];
							var rs = baseInfoForXlsSelectGrid
									.getSelectionModel().getSelections();
							for (var i = 0; i < length; i++) {
								var exPublicId = rs[i].data.exPublicId;
								exPublicIds.push(exPublicId);
							}
							Ext.Ajax.request({
								url : "ExeclCustomize!deleteExcelPublicMaster.action?exPublicIds="
										+ Ext.encode(exPublicIds),
								method : "post",
								success : function(response, options) {
									baseInfoForXlsSelectStore.reload();
									Ext.Msg.alert('提示', '删除操作成功！');
								}
							});
						}
						if (btn == "cancel") {
							return;
						}
					}
				});

			}
		}]

	});

	// 子表显示
	// ExeclCustomize!queryPublicMasterByCondition.action

	baseInfoForXlsSelectStore = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : 'ExeclCustomize!queryPublicMasterByCondition.action'
						}),
				reader : new Ext.data.JsonReader({
							root : 'publicMaster.result',
							totalProperty : 'publicMaster.totalCount'
						}, [{
									name : 'exPublicId',
									mapping : "exPublicId"
								}, {
									name : 'exPublicName',
									mapping : "exPublicName"
								}, {
									name : 'isUsed',
									mapping : "isUsed"
								}, {
									name : 'exMemo',
									mapping : "exMemo"
								}, {
									name : 'exPublicType',
									mapping : 'exPublicType'
								}])
			});
	baseInfoForXlsSelectStore.load();
	/**
	 * new Ext.data.SimpleStore({ fields : [{ name : 'exPublicId', type : "int" }, {
	 * name : 'exPublicName', type : "string" }, { name : 'isUsed', type : "int" }, {
	 * name : 'exMemo', type : "string" }, { name : 'exPublicType', type :
	 * 'string' }] });
	 * baseInfoForXlsSelectStore.loadData(Ext.baseInfoForXlsImport.publicMasterStore);
	 */

	// 查询显示的表格
	var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), sm, {
		header : '固定项目名称',
		dataIndex : 'exPublicName',
		width : 150,
		renderer : function(value, p, record) {
			return "<a href='#' onclick=\"modifyBaseInfo('"
					+ record.data.exPublicId + "','" + record.data.exPublicName
					+ "','" + record.data.isUsed + "','" + record.data.exMemo
					+ "','" + record.data.exPublicType + "');return false;\">"
					+ value + "</a>";

		}
	}, {
		header : 'excel文件类别',
		dataIndex : 'exPublicType',
		width : 150,
		renderer : function(v, p, record) {
			if (v == 1013) {
				return '雇员信息';
			} else if (v == 1015) {
				return '薪资类别';
			} else if (v == 1017) {
				return '社保项目';
			}
		}
	}, {
		header : '是否有效',
		dataIndex : 'isUsed',
		width : 150,
		renderer : function(v, p, record) {
			if (v == 1) {
				return '是';
			} else if (v == 0) {
				return '否';
			}
		}
	}, {
		header : '说明',
		dataIndex : 'exMemo',
		width : 150
	}]);

	baseInfoForXlsSelectPanel.render(Ext.getBody());
	var baseInfoForXlsSelectGrid = new Ext.grid.GridPanel({
				height : document.body.clientHeight
						- Ext.getCmp('addPanelForm').getInnerHeight()
						- Ext.getCmp('addPanelForm').getFrameHeight(),
				
				id : 'pbEmp',
				bbar : new Ext.PagingToolbar({
							pageSize : pageSize,
							store : baseInfoForXlsSelectStore,
							displayInfo : true,
							dispalyMsg : '显示第{0}条到{1}条记录，一共{2}条',
							emptyMsg : '没有记录'
						}),
				cm : cm,
				sm : sm,
				store : baseInfoForXlsSelectStore,
				loadMask : true,
				loadMask : {
					msg : '正在加载数据...'
				}
			});

	baseInfoForXlsSelectGrid.render(Ext.getBody());
});

function modifyBaseInfo(exPublicId, exPublicName, isUsed, exMemo, exPublicType) {
	baseInfoForXlsImportStore.baseParams = {
		exPublicid : exPublicId != null ? exPublicId : -1,
		exPublicType:exPublicType
	}
	baseInfoForXlsImportWin.findById('ex_publictype_new').setDisabled(false);
	baseInfoForXlsImportStore.modified = [];
	baseInfoForXlsImportStore.reload();
	baseInfoForXlsImportWin.findById('exPublicId').setValue(exPublicId);
	baseInfoForXlsImportWin.findById('exPublicName_new').setValue(exPublicName);
	baseInfoForXlsImportWin.findById('isUsed_id').setValue(isUsed);
	baseInfoForXlsImportWin.findById('exMemoMaster').setValue(exMemo);
	baseInfoForXlsImportWin.findById('ex_publictype_new')
			.setValue(exPublicType);
	if (exPublicType != null && exPublicType != "") {
		baseInfoForXlsImportWin.findById('ex_publictype_new').setDisabled(true);
	}
	baseInfoForXlsImportWin.show();
}
